package com.arelikebrothers.demo.concurrent.jdk;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by lennon on 01/05/2017.
 */
public class FairLock implements Runnable {
    // new 的时候传入参数true表示公平锁
    public static ReentrantLock fairLock = new ReentrantLock(true);

    @Override
    public void run() {
        while (true) {

            try {
                fairLock.lock();
                System.out.println(Thread.currentThread().getName() + "获得锁");
            } finally {
                fairLock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        FairLock lock = new FairLock();
        Thread t1 = new Thread(lock, "Thread-1");
        Thread t2 = new Thread(lock, "Thread-2");

        t1.start();
        t2.start();
    }
}
